package com.hhl.bookstore.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.hhl.bookstore.dao.CustomerDao;
import com.hhl.bookstore.domain.Book;
import com.hhl.bookstore.domain.Customer;
import com.hhl.bookstore.util.JdbcUtil;

public class CustomerDaoMySQLImpl implements CustomerDao {

	@Override
	public void save(Customer c) {
		
		if(c==null)
			throw new IllegalArgumentException();
		
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		
		
		try{
			conn = com.hhl.bookstore.util.JdbcUtil.getConnection();
			
			
			
			stmt = conn.prepareStatement("insert into customer (id,username,password,phone,address,email,actived,code) values(?,?,?,?,?,?,?,?)");
			stmt.setString(1, c.getId());
			stmt.setString(2, c.getUsername());
			stmt.setString(3, c.getPassword());
			stmt.setString(4, c.getPhone());
			stmt.setString(5, c.getAddress());
			stmt.setString(6, c.getEmail());
			stmt.setBoolean(7, c.isActived());
			stmt.setString(8, c.getCode());
			stmt.executeUpdate();
		}catch(Exception e){
			throw new RuntimeException(e);
		}finally{
			JdbcUtil.release(rs, stmt, conn);
		}
	}

	
	
	
	@Override
	public Customer find(String username, String password) {
		
		if(username==null && password == null)
			throw new IllegalArgumentException();
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		
		try{
			conn = JdbcUtil.getConnection();
			stmt = conn.prepareStatement("select id,username,password,phone,address,email,actived,code from customer where username=? and password=?");
			stmt.setString(1, username);
			stmt.setString(2, password);
			rs = stmt.executeQuery();
			if(rs.next()){
				Customer customer = new Customer();
				customer.setId(rs.getString("id"));
				customer.setUsername(rs.getString("username"));
				customer.setPassword(rs.getString("password"));
				customer.setPhone(rs.getString("phone"));
				customer.setAddress(rs.getString("address"));
				customer.setEmail(rs.getString("email"));
				customer.setActived(rs.getBoolean("actived"));
				customer.setCode(rs.getString("code"));
				return customer;
			}else
				return null;
		}catch(Exception e){
			e.printStackTrace();
			throw new RuntimeException(e);
		}finally{
			JdbcUtil.release(rs, stmt, conn);
		}
	}

	@Override
	public Customer findByCode(String username, String code) {

		if(username==null && code == null)
			throw new IllegalArgumentException();
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		
		try{
			conn = JdbcUtil.getConnection();
			stmt = conn.prepareStatement("select id,username,password,phone,address,email,actived,code from customer where username=? and code=?");
			stmt.setString(1, username);
			stmt.setString(2, code);
			rs = stmt.executeQuery();
			if(rs.next()){
				Customer customer = new Customer();
				customer.setId(rs.getString("id"));
				customer.setUsername(rs.getString("username"));
				customer.setPassword(rs.getString("password"));
				customer.setPhone(rs.getString("phone"));
				customer.setAddress(rs.getString("address"));
				customer.setEmail(rs.getString("email"));
				customer.setActived(rs.getBoolean("actived"));
				customer.setCode(rs.getString("code"));
				return customer;
			}else
				return null;
		}catch(Exception e){
			e.printStackTrace();
			throw new RuntimeException(e);
		}finally{
			JdbcUtil.release(rs, stmt, conn);
		}
	}

	@Override
	public Customer findById(String customerId) {
		

		if(customerId == null)
			throw new IllegalArgumentException();
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		
		try{
			conn = JdbcUtil.getConnection();
			stmt = conn.prepareStatement("select id,username,password,phone,address,email,actived,code from customer  where customerId=?");
			stmt.setString(1, customerId);
			rs = stmt.executeQuery();
			if(rs.next()){
				Customer customer = new Customer();
				customer.setId(rs.getString("id"));
				customer.setUsername(rs.getString("username"));
				customer.setPassword(rs.getString("password"));
				customer.setPhone(rs.getString("phone"));
				customer.setAddress(rs.getString("address"));
				customer.setEmail(rs.getString("email"));
				customer.setActived(rs.getBoolean("actived"));
				customer.setCode(rs.getString("code"));
				return customer;
			}else
				return null;
		}catch(Exception e){
			e.printStackTrace();
			throw new RuntimeException(e);
		}finally{
			JdbcUtil.release(rs, stmt, conn);
		}
	}

	@Override
	public void update(Customer c) {
		

		if(c==null)
			throw new IllegalArgumentException();
		
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		
		try{
			conn = com.hhl.bookstore.util.JdbcUtil.getConnection();
			
			stmt = conn.prepareStatement("update customer set username=?,password=?,phone=?,address=?,email=?,actived=?,code=? where id=?");
			stmt.setString(1, c.getUsername());
			stmt.setString(2, c.getPassword());
			stmt.setString(3, c.getPhone());
			stmt.setString(4, c.getAddress());
			stmt.setString(5, c.getEmail());
			stmt.setBoolean(6, c.isActived());
			stmt.setString(7, c.getCode());
			stmt.setString(8, c.getId());
			stmt.executeUpdate();
		}catch(Exception e){
			throw new RuntimeException(e);
		}finally{
			JdbcUtil.release(rs, stmt, conn);
		}
		
	}

}
